package com.jwthhealth_pub.wristband.newbloodpressure.view;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.MPPointF;
import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.wristband.newbloodpressure.utils.MyAxisValueFormatter;
import com.jwthhealth_pub.wristband.newbloodpressure.utils.XYMarkerView;
import com.jwthhealth_pub.wristband.newbloodpressure.utils.XinLvValueFormatter;
import com.jwthhealth_pub.wristband.newbloodpressure.utils.XueYaValueFormatter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 当前脉搏血压状态
 * Created by yang on 2017/12/15.
 * http://www.cnblogs.com/wangfeng520/p/5984077.html 详细的文档
 */

public class NewBloodPressureActivity extends BaseActivity implements
        OnChartValueSelectedListener {

    private final String TAG = makeLogTag(NewBloodPressureActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    protected BarChart mChartXueya;
    protected BarChart mChartXinLv;
    String xinlvValue,shousouyaVale,shuzhangyaValue, shuzhangya_normal,shousuoya_normal,xinlv_normal="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newbloodpressure);
        ButterKnife.bind(this);

        xinlvValue = getIntent().getStringExtra("xinlvValue");
        shousouyaVale = getIntent().getStringExtra("shousouyaVale");
        shuzhangyaValue = getIntent().getStringExtra("shuzhangyaValue");
        shuzhangya_normal = getIntent().getStringExtra("shuzhangya_normal");
        shousuoya_normal = getIntent().getStringExtra("shousuoya_normal");
        xinlv_normal = getIntent().getStringExtra("xinlv_normal");

        initBloodPressureBarDatas(); //血压的柱状图

        initXinLvDatas(); //心率的柱状图
    }

    private void initBloodPressureBarDatas() {

        mChartXueya = (BarChart) findViewById(R.id.chartXueya);
        mChartXueya.setOnChartValueSelectedListener(this);
        mChartXueya.animateY(1800, Easing.EasingOption.Linear);
        mChartXueya.animateX(1800, Easing.EasingOption.Linear);
        mChartXueya.setDrawBarShadow(false);
        mChartXueya.setDrawValueAboveBar(true);

        mChartXueya.getDescription().setEnabled(false);
        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        mChartXueya.setMaxVisibleValueCount(300);

        // scaling can now only be done on x- and y-axis separately
        mChartXueya.setPinchZoom(false);

        mChartXueya.setDrawGridBackground(false);//是否显示网格
        // mChart.setDrawYLabels(false);
//        mChartXueya.setDrawLabels(false);//设置为true打开绘制轴的标签。
//        mChartXueya.setDrawAxisLine(false); //设置为true，绘制轴线
//        mChartXueya.setDrawGridLines(false); //设置为true绘制网格线。

        IAxisValueFormatter xAxisFormatter = new XueYaValueFormatter(mChartXueya);

        XAxis xAxis = mChartXueya.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//        xAxis.setTypeface(mTfLight);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(xAxisFormatter);

        IAxisValueFormatter custom = new MyAxisValueFormatter();

        YAxis leftAxis = mChartXueya.getAxisLeft();
//        leftAxis.setTypeface(mTfLight);
        leftAxis.setLabelCount(8, false);
        leftAxis.setValueFormatter(custom);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        leftAxis.setDrawGridLines(false);//是否画线
        leftAxis.setEnabled(false);

        YAxis rightAxis = mChartXueya.getAxisRight();
        rightAxis.setDrawGridLines(false);
//        rightAxis.setTypeface(mTfLight);
        rightAxis.setLabelCount(8, false);
        rightAxis.setValueFormatter(custom);
        rightAxis.setSpaceTop(15f);
        rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        rightAxis.setDrawGridLines(false);//是否画线
        rightAxis.setEnabled(false);

        Legend l = mChartXueya.getLegend();
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
//        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//        l.setDrawInside(false);
//        l.setForm(Legend.LegendForm.SQUARE);
//        l.setFormSize(9f);
//        l.setTextSize(11f);
//        l.setXEntrySpace(4f);
        l.setEnabled(false);

        XYMarkerView mv = new XYMarkerView(this, xAxisFormatter);
        mv.setChartView(mChartXueya); // For bounds control
        mChartXueya.setMarker(mv); // Set the marker to the chart
        mChartXueya.setBorderWidth(50f);
        setXueyaData(3, 30);
    }


    private void initXinLvDatas() {

        mChartXinLv = (BarChart) findViewById(R.id.chartXinLv);
        mChartXinLv.setOnChartValueSelectedListener(this);
        mChartXinLv.animateY(1800, Easing.EasingOption.Linear);
        mChartXinLv.animateX(1800, Easing.EasingOption.Linear);
        mChartXinLv.setDrawBarShadow(false);
        mChartXinLv.setDrawValueAboveBar(true);

        mChartXinLv.getDescription().setEnabled(false);
        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        mChartXinLv.setMaxVisibleValueCount(300);

        // scaling can now only be done on x- and y-axis separately
        mChartXinLv.setPinchZoom(false);

        mChartXinLv.setDrawGridBackground(false);//是否显示网格
        // mChart.setDrawYLabels(false);

        IAxisValueFormatter xAxisFormatter = new XinLvValueFormatter(mChartXinLv);

        XAxis xAxis = mChartXinLv.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//        xAxis.setTypeface(mTfLight);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(xAxisFormatter);

        IAxisValueFormatter custom = new MyAxisValueFormatter();

        YAxis leftAxis = mChartXinLv.getAxisLeft();
//        leftAxis.setTypeface(mTfLight);
        leftAxis.setLabelCount(8, false);
        leftAxis.setValueFormatter(custom);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        leftAxis.setDrawGridLines(false);//是否画线
        leftAxis.setEnabled(false);

        YAxis rightAxis = mChartXinLv.getAxisRight();
        rightAxis.setEnabled(false);
        rightAxis.setDrawGridLines(false);
//        rightAxis.setTypeface(mTfLight);
        rightAxis.setLabelCount(8, false);
        rightAxis.setValueFormatter(custom);
        rightAxis.setSpaceTop(15f);
        rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        rightAxis.setDrawGridLines(false);//是否画线

        mChartXinLv.getLegend().setEnabled(false);

        XYMarkerView mv = new XYMarkerView(this, xAxisFormatter);
        mv.setChartView(mChartXinLv); // For bounds control
        mChartXinLv.setMarker(mv); // Set the marker to the chart
        mChartXinLv.setBorderWidth(50f);
        setXinLvData(1, 30);
    }

    private void setXinLvData(int count, float range) {

        float start = 1f;

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

            if(!xinlv_normal.isEmpty()){
                yVals1.add(new BarEntry(1,Integer.valueOf(xinlv_normal)));
            }
            if(!xinlvValue.isEmpty()){
                yVals1.add(new BarEntry(2,Integer.valueOf(xinlvValue)));
            }


        yVals1.add(new BarEntry(3,80));
        yVals1.add(new BarEntry(4,90));

        BarDataSet set1;  //这就是每个柱状图的实例

        if (mChartXinLv.getData() != null &&
                mChartXinLv.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mChartXinLv.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mChartXinLv.getData().notifyDataChanged();
            mChartXinLv.notifyDataSetChanged();


        } else {
            set1 = new BarDataSet(yVals1, "心率");
            set1.setDrawIcons(false);

            int[] PASTEL_COLORS = {  //设置每个柱状图背景色
                    Color.rgb(158, 140, 213), Color.rgb(75, 204, 209), Color.rgb(158, 140, 213),
                    Color.rgb(75, 204, 209)
            };
            set1.setColors(PASTEL_COLORS);

            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(13f);//调节顶部字体大小
//            data.setValueTextColors(colorList);
            data.setValueTextColor(R.color.primarycolor);
//            data.setValueTypeface(mTfLight);
            data.setBarWidth(0.14f); //调节间距的

            mChartXinLv.setData(data);
        }
    }


    private void setXueyaData(int count, float range) {

        float start = 1f;

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
//这里是血压的赋值
            if(!shousuoya_normal.isEmpty()){
                yVals1.add(new BarEntry(1,Integer.valueOf(shuzhangya_normal)));
            }
            if(!shuzhangya_normal.isEmpty()){
                yVals1.add(new BarEntry(2,Integer.valueOf(shuzhangya_normal)));
            }
            if(!shuzhangyaValue.isEmpty()){
                yVals1.add(new BarEntry(3,Integer.valueOf(shuzhangyaValue)));
            }
            if(!shousouyaVale.isEmpty()){
                yVals1.add(new BarEntry(4,Integer.valueOf(shousouyaVale)));
            }

        BarDataSet set1;  //这就是每个柱状图的实例

        if (mChartXueya.getData() != null &&
                mChartXueya.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mChartXueya.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mChartXueya.getData().notifyDataChanged();
            mChartXueya.notifyDataSetChanged();


        } else {
            set1 = new BarDataSet(yVals1, "血压");
            set1.setDrawIcons(false);

            int[] PASTEL_COLORS = {  //设置每个柱状图背景色
                    Color.rgb(158, 140, 213), Color.rgb(75, 204, 209), Color.rgb(158, 140, 213),
                    Color.rgb(75, 204, 209)
            };
            set1.setColors(PASTEL_COLORS);

            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(13f);//调节顶部字体大小
//            data.setValueTextColors(colorList);
            data.setValueTextColor(R.color.primarycolor);
//            data.setValueTypeface(mTfLight);
            data.setBarWidth(0.14f); //调节间距的

            mChartXueya.setData(data);
        }
    }

    protected RectF mOnValueSelectedRectF = new RectF();

    @SuppressLint("NewApi")
    @Override
    public void onValueSelected(Entry e, Highlight h) {

        if (e == null)
            return;

        RectF bounds = mOnValueSelectedRectF;
        mChartXueya.getBarBounds((BarEntry) e, bounds);
        MPPointF position = mChartXueya.getPosition(e, YAxis.AxisDependency.LEFT);

        Log.i("bounds", bounds.toString());
        Log.i("position", position.toString());

        Log.i("x-index",
                "low: " + mChartXueya.getLowestVisibleX() + ", high: "
                        + mChartXueya.getHighestVisibleX());

        MPPointF.recycleInstance(position);
    }

    @Override
    public void onNothingSelected() { }


    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }




}
